package com.sp.system.user.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Describle  String 字符串工具类
 * @author chenanhai
 * @Date 2019年9月26日
 */
@Slf4j
public class StringUtil {

    private final static String[] areaReg = {"#楼","号楼","-"};
    private final static String numReg = "\\d+";
    private final static String cnNumReg = "[一,二,三,四,五,六,七,八,九,十]{1,4}";

    /**
     * 获取字符串中数字 组装成map key位为数字结束位置
     * @param content
     * @return
     */
    public static Map<Integer,String> areaSplit(String content){
        Map<Integer,String> map = new HashMap<Integer,String>();

        content = content.trim();
        Pattern pattern = Pattern.compile(numReg);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            map.put(matcher.end(),matcher.group(0));
        }
        return map;
    }
    /**
     * 获取字符串中数字 组装成map key位为数字结束位置
     * @param content
     * @return
     */
    public static Map<Integer,String> areaCNSplit(String content){
        Map<Integer,String> map = new HashMap<>();

        content = content.trim();
        Pattern pattern = Pattern.compile(cnNumReg);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            map.put(matcher.end(),cnToNum(matcher.group(0)));
        }
        return map;
    }


    /**
     * 中文数字转换 阿拉伯数字
     * @param cn
     * @return
     */
    public static String cnToNum(String cn){
        int num = 0;
        String totalNum = "";
        if(cn == null || "".equals(cn)){
            return totalNum;
        }
        if(cn.length() == 1){//一
            totalNum = transformatNum(cn) + "";
        } else if(cn.length() == 2){//十一
            String[] transNums = cn.split("");

            num = transformatNum(transNums[0]);
            if(num == 10){
                totalNum = 10 + transformatNum(transNums[1]) + "";
            }
        } else if(cn.length() > 2 && cn.length() < 9){ //三百一十二  三千四百二十八
            cn = cn.replaceAll("[十百千万]","");
            String[] transNums = cn.split("");

            for(int i = 0; i < cn.length(); i++){
                num = transformatNum(transNums[i]);
                totalNum = totalNum + num;
            }
        }

        return totalNum+"";

    }

    /**
     * 获取小区
     * @param area
     * @return
     */
    public static String getArea(String area){
        if(area == null || "".equals(area)){
            return area;
        }
        Map<Integer,String> contentNum = areaSplit(area);
        Map<Integer,String> contentCN = areaCNSplit(area);

        Map<Integer, String> content = contentNum.entrySet()
                .stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (v1, v2) -> v1,
                        () -> new HashMap<>(contentCN)));
        log.debug("小区划分##########{} {}",content);
        for(Map.Entry<Integer,String> entry: content.entrySet()){
            int key = entry.getKey();
            String value = entry.getValue();

            if(key < area.length()-2){
                String nextValue = area.substring(key,key+2);
                String nextOneValue = area.substring(key,key+1);
                if(Arrays.asList(areaReg).contains(nextValue)){
                    log.debug("小区划分##########{} {}",value);
                    return value + areaReg[1];
                }
                if(Arrays.asList(areaReg).contains(nextOneValue)){
                    log.debug("小区划分##########{} {}",value);
                    return value + areaReg[1];
                }
            }
        }
        return null;
    }

    public static int transformatNum(String cn){
        int num = 0;
        switch (cn){
            case"一" :
                num = 1;
                break;
            case"二" :
                num = 2;
                break;
            case"三" :
                num = 3;
                break;
            case"四" :
                num = 4;
                break;
            case"五" :
                num = 5;
                break;
            case"六" :
                num = 6;
                break;
            case"七" :
                num = 7;
                break;
            case"八" :
                num = 8;
                break;
            case"九" :
                num = 9;
                break;
            case"十":
                num = 10;
                break;
            case"百":
                num = 100;
                break;
            default :
                num = 0;
        }
        return num;
    }


}
